package com.appspot.showup_app.showup;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.appspot.showup_app.showup.model.ShowUpBusinessMessage;
import com.appspot.showup_app.showup.tasks.UpdateBusinessInfoTask;

public class EditBusinessInfoActivity  extends ActionBarActivity {
    private static final String LOG_TAG = "EditBusinessInfoActivity";
    private static final String TAG = "Showup";
    EditText editName;
    EditText editBusinessName;
    EditText editPhoneNumber;
    EditText editEmail;
    EditText editAddress;
    EditText editNotificationMessage;
    EditText editNotificationTime;
    SharedPreferences sharedPreferences;
    ShowUpBusinessMessage business;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_business_info);

        if (!ApplicationData.getInstance().isLoggedIn())
        {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }

        editName = (EditText) findViewById(R.id.editName);
        editBusinessName = (EditText) findViewById(R.id.editBusinessName);
        editPhoneNumber = (EditText) findViewById(R.id.editPhoneNumber);
        editEmail = (EditText) findViewById(R.id.editEmail);
        editAddress = (EditText) findViewById(R.id.editAddress);
        editNotificationMessage = (EditText) findViewById(R.id.editNotificationMessage);
        editNotificationTime = (EditText) findViewById(R.id.editNotificationTime);

        sharedPreferences = getSharedPreferences(TAG, 0);
        business = DatabaseHelper.getBusiness(sharedPreferences);

        editName.setText(business.getName());
        editBusinessName.setText(business.getBusinessName());
        editPhoneNumber.setText(business.getPhoneNumber());
        editEmail.setText(business.getEmail());
        editAddress.setText(business.getAddress());
        editNotificationMessage.setText(business.getNotificationMessage());
        editNotificationTime.setText(String.valueOf(business.getNotificationTime()));

    }

    public void onClickEditBusinessInfo(View view) {
        business.setEmail(editEmail.getText().toString());
        business.setNotificationTime(Long.parseLong(editNotificationTime.getText().toString()));
        business.setName(editName.getText().toString());
        business.setAddress(editAddress.getText().toString());
        business.setPhoneNumber(editPhoneNumber.getText().toString());
        business.setBusinessName(editBusinessName.getText().toString());
        business.setNotificationMessage(editNotificationMessage.getText().toString());

        UpdateBusinessInfoTask updateBusinessInfoTask = new UpdateBusinessInfoTask(this, business) {
            @Override
            protected void onPostExecute(ShowUpBusinessMessage newBusinessMessage) {
                if (newBusinessMessage != null) {
                    DatabaseHelper.updateBusinessInfo(sharedPreferences, newBusinessMessage);
                    setResult(RESULT_OK, new Intent());
                    finish();
                }
                else {
                    Toast.makeText(EditBusinessInfoActivity.this, "Error updating", Toast.LENGTH_LONG);
                }
            }
        };
        updateBusinessInfoTask.execute((Void)null);

    }
}
